package models

import (
	"errors"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"project/app/electromechanical/models/dto"
	orm "project/common/global"
	"time"
)
//奖学金实体
type SysShip struct {
	Id         int 		`json:"id"`		//id
	Studentid  int 		`json:"studentid"`//学生ID
	Time      string 	`json:"time"`	//时间
	Level     string 	`json:"level"`	//级别
	Grade	  string 	`json:"grade"`	//年级
 	Name       string `json:"name"`       //姓名
}
type Stu struct {
	Number         string 		`json:"number"`
	Password      string  `json:"password"`
	Phone        string   `json:"phone"`
}

//学生实体
type SysStu struct {
	*BaseModel
	Name       string `json:"name"`       //姓名
	Year       string `json:"year"`
	QQ         string `json:"qq"`
	VX         string `json:"vx"`
	Place      string `json:"place"`      //家庭住址
	Gender	   string `json:"gender"`     //性别
	Major      string `json:"major"`      //专业
	Class      string `json:"class"`      //班级
	Dormitory  string `json:"dormitory"`  //宿舍号
	Number     string `json:"number"`     //学号
	Phone      string `json:"phone"`      //手机号
	Nation     string `json:"nation"`     //民族
	Committee  string `json:"committee"`  //班委
	NativePlace string `json:"native_place"`//籍贯
	PoliticalOutlook string `json:"political_outlook"` //政治面貌
	WatchmanName  string `json:"watchman_name"`//监护人姓名
	WatchmanRelationship string `json:"watchman_relationship"`//监护人关系
	WatchmanPhone  string `json:"watchman_phone"`//监护人手机号
	Remarks  string `json:"remarks"`//备注
	StudentUnion string `json:"student_union"`//学生会
	AcademicWarning string `json:"academic_warning"`//学业警示
	State      string `json:"state"`      //状态
	PsychologicalWarning string `json:"psychological_warning"`//心里预警
	Team       string `json:"team"`       //球队
	Picture    string `json:"picture"`   //照片
}
//奖学金实体
type Scholarship struct {
	Name         string 	`json:"name"`//学生姓名
	Number       string 	`json:"number"`
	Class		 string 	`json:"class"`
	Time         string 	`json:"time"`
	Level        string 	`json:"level"`
	Grade		 string 	`json:"grade"`
}
//获奖信息实体
type Insegrals struct {
	Studentid    uint 		`json:"studentid"`
	Class        string 	`json:"class"`
	Number       string 	`json:"number"`
	Time         time.Time 	`json:"time"`
	Name         string      `json:"name"`
	InName       string 	`json:"in_name"`
	Level        string 	`json:"level"`
	Insegral	 string 	`json:"insegral"`
	InPoints	 uint 		`json:"in_points"`
	IsDel       []byte 		`json:"is_del"`
	Dels        []byte 		`json:"dels"`
}
//获奖信息实体
type Noinsegrals struct {
	Number       string 	`json:"number"`
	Class        string 	`json:"class"`
	Studentid    uint 		`json:"studentid"`
	Time         time.Time 	`json:"time"`
	Name         string      `json:"name"`
	NoName       string 	`json:"no_name"`
	Level        string 	`json:"level"`
	Nosegral	 string 	`json:"nosegral"`
	NoPoints	 uint 		`json:"no_points"`
}
//添加奖励实体
type Insegral struct {
	Id           uint 		`json:"id"`
	Time         time.Time 	`json:"time"`
	Studentid    uint 		`json:"studentid"`
	InName       string 	`json:"in_name"`
	Level        string 	`json:"level"`
	Insegral	 string 	`json:"insegral"`
	InPoints	 uint 		`json:"in_points"`
	IsDel       []byte 		`json:"is_del"`
	Dels        []byte 		`json:"dels"`
}
//添加惩罚实体
type Noinsegral struct {
	Studentid    uint 		`json:"studentid"`
	Time         time.Time 	`json:"time"`
	NoName       string 	`json:"no_name"`
	Level        string 	`json:"level"`
	Nosegral	 string 	`json:"nosegral"`
	NoPoints	 uint 		`json:"no_points"`
}
//删除奖励实体
type DelInsegral struct {
	Id    uint 		`json:"id"`
	IsDel uint 	`json:"is_del"`
}
//班委实体
type Committee struct {
	Id    uint 		`json:"id"`
	ComName string `json:"com_name"`
}
type Login struct {
	Number    string 		`json:"number"`
	Password string `json:"password"`
}





//GetStuList  查询学生列表
func (e *SysStu) GetStuList(p *dto.GetStuList)(stuList *[]SysStu,err error)  {
	stuList = new([]SysStu)
	err = orm.Eloquent.Table("stu").Where("is_del = 0").Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(&stuList).Error
	return
}
//GetStuDetails id查询学生信息
func (e *SysStu) GetStuDetails(p *dto.GetStuDetails)(stuDetails *[]SysStu,err error)  {
	stuDetails = new([]SysStu)
	table := orm.Eloquent.Table("stu").Where("is_del = 0")
	err = table.Where("id=?",p.Id).Find(stuDetails).Error
	return
}
//GetStuNumber  学号查询学生列表
func (e *SysStu) GetStuNumber(p *dto.GetStuNumber)(stuNumber *[]SysStu,err error)  {
	stuNumber = new([]SysStu)
	err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("number=?",p.Number).Find(&stuNumber).Error
	return
}
//GetStuLists 条件查询学生列表
func (e *SysStu) GetStuLists(p *dto.GetStuLists)(stuLists *[]SysStu,err error)  {
	stuLists = new([]SysStu)
	if (p.Year!="0"){
		if(p.Class=="0"&&p.Grade=="0"&&p.Major=="0"&&p.State=="0"){  				 //通过年级搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("year=?",p.Year).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade=="0"&&p.Major=="0"&&p.State=="0"){    		 //通过年级班级搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND year=?",p.Class,p.Year).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
				return
 		}else if(p.Class=="0"&&p.Grade!="0"&&p.Major=="0"&&p.State=="0"){			//通过年级性别搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("grade=? AND year=?",p.Grade,p.Year).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class=="0"&&p.Grade=="0"&&p.Major!="0"&&p.State=="0"){			//通过年级专业搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("major=? AND year=?",p.Major,p.Year).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class=="0"&&p.Grade=="0"&&p.Major=="0"&&p.State!="0"){			//通过年级状态搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("state=? AND year",p.State,p.Year).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade!="0"&&p.Major=="0"&&p.State=="0"){			//通过年级班级性别搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND grade AND year=?",p.Class,p.Grade,p.Year).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade=="0"&&p.Major!="0"&&p.State=="0"){			//通过年级班级状态搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND major AND year=?",p.Class,p.Major,p.Year).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade=="0"&&p.Major=="0"&&p.State!="0"){			//通过年级班级状态搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND state AND year=?",p.Class,p.State,p.Year).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade!="0"&&p.Major!="0"&&p.State=="0"){
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND grade AND major AND year=?",p.Class,p.Grade,p.Major,p.Year).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade!="0"&&p.Major=="0"&&p.State!="0"){
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND state AND grade AND year=?",p.Class,p.State,p.Grade,p.Year).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade!="0"&&p.Major!="0"&&p.State!="0"){
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND grade AND major AND state AND year",p.Class,p.Grade,p.Major,p.State,p.Year).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}
	}else {
		if(p.Class=="0"&&p.Grade=="0"&&p.Major=="0"&&p.State=="0"){  				 //通过年级搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("id>0").Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade=="0"&&p.Major=="0"&&p.State=="0"){    		 //通过年级班级搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? ",p.Class).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class=="0"&&p.Grade!="0"&&p.Major=="0"&&p.State=="0"){			//通过年级性别搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("grade=?",p.Grade).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class=="0"&&p.Grade=="0"&&p.Major!="0"&&p.State=="0"){			//通过年级专业搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("major=?",p.Major).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class=="0"&&p.Grade=="0"&&p.Major=="0"&&p.State!="0"){			//通过年级状态搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("state=? ",p.State).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade!="0"&&p.Major=="0"&&p.State=="0"){			//通过年级班级性别搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND grade",p.Class,p.Grade).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade=="0"&&p.Major!="0"&&p.State=="0"){			//通过年级班级状态搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND major",p.Class,p.Major).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade=="0"&&p.Major=="0"&&p.State!="0"){			//通过年级班级状态搜索
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND state",p.Class,p.State).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade!="0"&&p.Major!="0"&&p.State=="0"){
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND grade AND major",p.Class,p.Grade,p.Major).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade!="0"&&p.Major=="0"&&p.State!="0"){
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND state AND grade",p.Class,p.State,p.Grade).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}else if(p.Class!="0"&&p.Grade!="0"&&p.Major!="0"&&p.State!="0"){
			err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("class=? AND grade AND major AND state ",p.Class,p.Grade,p.Major,p.State).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuLists).Error
			return
		}

	}
return
}
//GetStuTeam 查询球队列表
func (e *SysStu) GetStuTeam(p *dto.GetStuTeam)(stuTeam *[]SysStu,err error)  {
	stuTeam = new([]SysStu)
	err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("team!=暂无").Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuTeam).Error
	return
}
//GetStuWarning 查询心里预警列表
func (e *SysStu) GetStuWarning(p *dto.GetStuWarning)(stuWarning *[]SysStu,err error)  {
	stuWarning = new([]SysStu)
	err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("psychological_warning=1").Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuWarning).Error
	return
}
//GetStuCommittee 查询班委列表
func (e *SysStu) GetStuCommittee(p *dto.GetStuCommittee)(stuCommittee *[]SysStu,err error)  {
	stuCommittee = new([]SysStu)
	err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("committee!=学生").Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuCommittee).Error
	return
}
//GetStuMember  查询党员列表
func (e *SysStu) GetStuMember(p *dto.GetStuMember)(stuMember *[]SysStu,err error)  {
	stuMember = new([]SysStu)
	err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("political_outlook=党员").Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuMember).Error
	return
}
//GetStuNation  查询少数民族列表
func (e *SysStu) GetStuNation(p *dto.GetStuNation)(stuNation *[]SysStu,err error)  {
	stuNation = new([]SysStu)
	err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("nation!=汉族").Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuNation).Error
	return
}
//GetStuUnion 查询学生会列表
func (e *SysStu) GetStuUnion(p *dto.GetStuUnion)(stuUnion *[]SysStu,err error)  {
	stuUnion = new([]SysStu)
	err = orm.Eloquent.Table("stu").Where("is_del = 0").Where("student_union!=学生").Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuUnion).Error
	return
}
//GetStuUnion 查询奖学金
func (e *SysShip) Scholarship(p *dto.Scholarship)(stuShip *[]SysShip,err error) {
	stuShip = new([]SysShip)
	if(p.Time=="0"&&p.Grade=="0"&&p.Level=="0"){
		table := orm.Eloquent.Table("stu_scholarship").Where("is_del = 0")
		err = table.Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuShip).Error
		return
	}else if(p.Time!="0"&&p.Grade=="0"&&p.Level=="0"){
		table := orm.Eloquent.Table("stu_scholarship").Where("is_del = 0")
		err = table.Where("time=?",p.Time).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuShip).Error
		return
	}else if(p.Time=="0"&&p.Grade!="0"&&p.Level=="0"){
		table := orm.Eloquent.Table("stu_scholarship").Where("is_del = 0")
		err = table.Where("grade=?",p.Grade).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuShip).Error
		return
	}else if(p.Time=="0"&&p.Grade=="0"&&p.Level!="0"){
		table := orm.Eloquent.Table("stu_scholarship").Where("is_del = 0")
		err = table.Where("level=?",p.Level).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuShip).Error
		return
	}else if(p.Time!="0"&&p.Grade!="0"&&p.Level=="0"){
		table := orm.Eloquent.Table("stu_scholarship").Where("is_del = 0")
		err = table.Where("time=? AND grade=?",p.Time,p.Grade).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuShip).Error
		return
	}else if(p.Time!="0"&&p.Grade=="0"&&p.Level!="0"){
		table := orm.Eloquent.Table("stu_scholarship").Where("is_del = 0")
		err = table.Where("time=? AND level=?",p.Time,p.Level).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuShip).Error
		return
	}else if(p.Time=="0"&&p.Grade!="0"&&p.Level!="0"){
		table := orm.Eloquent.Table("stu_scholarship").Where("is_del = 0")
		err = table.Where("grade=? AND level=?",p.Grade,p.Level).Limit(int(p.Size)).Offset(int((p.Current - 1) * p.Size)).Find(stuShip).Error
		return
	}
	return
}
// ShipListDownload 导出奖学金
func (e *Scholarship) ShipListDownload() (shipList []*Scholarship, err error) {
	err = orm.Eloquent.Table("stu_scholarship").
		Select("stu.name,stu.number,stu.class," +
			"stu_scholarship.time,stu_scholarship.level,stu_scholarship.grade").
		Joins("left join stu on stu.id = stu_scholarship.studentid").Find(&shipList).Error
	return
}
// Downinsegrals 导出荣誉
func (e *Insegrals) Downinsegrals() (inseList []*Insegrals, err error) {
	err = orm.Eloquent.Table("stu_insegrals").
		Select("stu.name,stu.number,stu.class," +
			"stu_insegrals.time,stu_insegrals.level,stu_insegrals.in_name," +
			"stu_insegrals.insegral").
		Joins("left join stu on stu.id = stu_insegrals.studentid").Find(&inseList).Error
	return
}
// Downnoinsegrals 导出惩罚
func (e *Noinsegrals) Downnoinsegrals() (noinseList []*Noinsegrals, err error) {
	err = orm.Eloquent.Table("stu_noinsegrals").
		Select("stu.name,stu.number,stu.class," +
			"stu_noinsegrals.time,stu_noinsegrals.level,stu_noinsegrals.no_name," +
			"stu_noinsegrals.insegral").
		Joins("left join stu on stu.id = stu_noinsegrals.studentid").Find(&noinseList).Error
	return
}
//添加奖励
func (e *Insegral) Addpoint () (err error) {
	err = orm.Eloquent.Table("stu_insegrals").Create(e).Error
	return
}
//添加惩罚
func (e *Noinsegral) Delpoint () (err error) {
	err = orm.Eloquent.Table("stu_noinsegrals").Create(e).Error
	return
}
//删除奖励
func (e *Insegral) Deladdpoint (p *dto.Deladdpoint) (err error) {
	err = orm.Eloquent.Begin().Table("stu_insegrals").Where("id in (?)", p.Id).Updates(&Insegral{IsDel:  []byte{0},}).Error
	return
}

// Login 查询用户是否存在，并验证密码
func (u *Login) Login() error {
	oPassword := u.Password
	err := orm.Eloquent.Table("stu_user").Where("number = ?", u.Number).First(u).Error
	if err == gorm.ErrRecordNotFound {
		zap.L().Error("用户不存在", zap.Error(err))
		return errors.New("用户不存在")
	}
	if u.Password != oPassword {
		zap.L().Error("user account or password is error")
		return errors.New("user account or password is error")
	}
	return nil
}







func (e *SysStu) Add () (err error) {
	err = orm.Eloquent.Table("stu").Create(e).Error
	return
}
func (a *Stu) Add1 () (err error) {
	err = orm.Eloquent.Table("stu_user").Create(a).Error
	return
}